/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.MyQuery;
import dao.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import pojo.User;

/**
 *
 * @author TrieuKhang
 */
@WebServlet(name = "ajax_register", urlPatterns = {"/ajax_register"})
public class ajax_register extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        PrintWriter out = response.getWriter();
        
        
        
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            
            //init json obj response
            JSONObject jObj_response = new JSONObject();

            //init json parser
            JSONParser jsonPaser = new JSONParser();
            JSONObject jsonObj = new JSONObject();


            try {
                jsonObj = (JSONObject) jsonPaser.parse(request.getParameter("data"));
            } catch (ParseException ex) {
                Logger.getLogger(ajax_register.class.getName()).log(Level.SEVERE, null, ex);
            }

            response.setContentType("text/x-json; charset=UTF-8");   
            
            request.setCharacterEncoding("UTF-8");
            
            String username = (String) jsonObj.get("username");
            String pass = (String) jsonObj.get("password");
  
            User user = new User();
            
            user.setUsername(username);
            user.setPassword(pass);
            
            //get list user
            boolean result = UserDAO.checkExists(user);
                        
            //có nghĩa là chưa có ai sử dụng username đó 
            if(result == false) {
                UserDAO.addUser(user);
            }
            
            boolean register_result = !result;
            
            jObj_response.put("result", register_result);            
           
            //tra json ra
            out.println(jObj_response);                        
            
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
